home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / db / esm-3.1 / esm-3 / usr / local / sm / src / diskproc / readDiskMessage.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-05  |  6.8 KB  |  304 lines

  1. /*
  2.  *   $RCSfile: readDiskMessage.c,v $  
  3.  *   $Revision: 1.2 $  
  4.  *   $Date: 1996/05/04 23:51:47 $      
  5.  */ 
  6. /**********************************************************************
  7. * EXODUS Database Toolkit Software
  8. * Copyright (c) 1991 Computer Sciences Department, University of
  9. *                    Wisconsin -- Madison
  10. * All Rights Reserved.
  11. *
  12. * Permission to use, copy, modify and distribute this software and its
  13. * documentation is hereby granted, provided that both the copyright
  14. * notice and this permission notice appear in all copies of the
  15. * software, derivative works or modified versions, and any portions
  16. * thereof, and that both notices appear in supporting documentation.
  17. *
  18. * THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY OF WISCONSIN --
  19. * MADISON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.  
  20. * THE DEPARTMENT DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES
  21. * WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  22. *
  23. * The EXODUS Project Group requests users of this software to return 
  24. * any improvements or extensions that they make to:
  25. *
  26. *   EXODUS Project Group 
  27. *     c/o David J. DeWitt and Michael J. Carey
  28. *   Computer Sciences Department
  29. *   University of Wisconsin -- Madison
  30. *   Madison, WI 53706
  31. *
  32. *     or exodus@cs.wisc.edu
  33. *
  34. * In addition, the EXODUS Project Group requests that users grant the 
  35. * Computer Sciences Department rights to redistribute these changes.
  36. **********************************************************************/
  37.  
  38. #include "queue_consist.h"
  39. #include "sysdefs.h"
  40. #include <sys/stat.h>
  41. #include "ess.h"
  42. #include "checking.h"
  43. #include "list.h"
  44. #include "error.h"
  45. #include "tid.h"
  46. #include "pool.h"
  47. #include "io.h"
  48. #include "bitvec.h"
  49. #include "lock.h"
  50. #include "object.h"
  51. #include "msgdefs.h"
  52. #include "disk.h"
  53. #include "thread.h"
  54. #include "semaphore.h"
  55. #include "latch.h"
  56. #include "bf.h"
  57. #include "link.h"
  58. #include "volume.h"
  59. #include "trace.h"
  60. #include "msgvector.h"
  61.  
  62. #include "disk_funcs.h"
  63. #include "queues.h"
  64. #include "diskproc_globals.h"
  65. #include "diskproc_intfuncs.h"
  66. #include "diskproc_extfuncs.h"
  67. #include "softwareVersion.h"
  68.  
  69. #ifdef linux
  70. #include <bsd/signal.h>
  71. #endif linux
  72.  
  73.  
  74. /*ARGSUSED*/
  75. static  void
  76. riseAndShine( 
  77. )
  78. {
  79.    /*
  80.     * See if parent(server) is still around 
  81.     * after having blocked on the semphore.
  82.     */
  83.     int pid;
  84.  
  85.  
  86.     TRPRINT(TR_DISKRW, TR_LEVEL_2, 
  87.         ("Alarm woke up disk process"));
  88. #ifdef hpux
  89.     if((pid = getpgrp2(Parent))<0) {
  90. #elif defined(linux)
  91.     if((pid = getpgrp())<0) {
  92. #else
  93.     if((pid = getpgrp(Parent))<0) {
  94. #endif
  95.         if(errno == ESRCH) { /* no such process */
  96.             pid = 1; /* as if inherited by init */
  97.         } else {
  98.             SM_ERROR(TYPE_FATAL, errno);
  99.         }
  100.     }
  101.     if(pid == 1) { 
  102.         /* 
  103.          * Parent is gone.  We were inherited by init(). 
  104.          * Server died so we'd better shut ourselves down.
  105.          * No need for a core file.
  106.          */
  107.         SM_ERROR(TYPE_STOP, esmSERVPROCDIED);
  108.     }
  109.     errno = EINTR; /* to be sure */
  110.  
  111. }
  112.  
  113.  
  114. static int
  115. rawDeviceSetup()
  116. {
  117.     struct stat     statInfo;
  118.  
  119.     /*
  120.      *  See if the named file is a raw disk
  121.      */
  122.     if (stat(DiskName, &statInfo) < 0) {
  123.         SM_ERROR(TYPE_SYS, errno);
  124.         return esmFAILURE; /* not ok */
  125.     }
  126.     /* if it's not a character device, its not a raw disk */
  127.     if ((statInfo.st_mode & S_IFMT) != S_IFCHR) {
  128.         RawDev = FALSE;
  129.         BlockBuf = NULL;
  130.     } else {
  131.         RawDev = TRUE;
  132.         BlockBufSize = MAX_IO_LIST * MIN_PAGESIZE;
  133.         BlockBuf = (char*) malloc(BlockBufSize + MIN_PAGESIZE/*align*/);
  134.         if (BlockBuf == NULL)  {
  135.             SM_ERROR(TYPE_SYS, esmMALLOCFAILED);
  136.             return (esmFAILURE);
  137.         }
  138.         /* align the buffer */
  139.         BlockBuf = (char*)
  140.             ( ((int) (BlockBuf+MIN_PAGESIZE)) & ~(MIN_PAGESIZE - 1));
  141.  
  142.     }
  143.     return(esmNOERROR);
  144. }
  145.  
  146.  
  147.  void
  148. readDiskMessage ()
  149.  
  150. {
  151.     DISKMSG                *message;
  152.  
  153.     struct sigvec         sv;
  154.     ShmOffset             shmoffset;
  155.  
  156.     /* signal(SIGALRM, riseAndShine); */
  157.     sv.sv_mask = 0;
  158.     sv.sv_flags = 0;
  159. #if defined(SIG_PFV) || defined(hpux) || defined(linux)
  160. /* Sun C++ and hpux define  SIG_PFV in sys/signal.h */
  161.     sv.sv_handler = (void (*)(...))riseAndShine;
  162. #else
  163.     sv.sv_handler = riseAndShine;
  164. #endif
  165.  
  166. #ifdef hpux
  167.     sigvector(SIGALRM, &sv, NULL);
  168. #else
  169.     sigvec(SIGALRM, &sv, NULL);
  170. #endif hpux
  171.  
  172.  
  173.     for (;;)    {
  174.  
  175.  
  176. #if MAGIC_CHECKING  IS_ENABLED
  177.         if(DiskQueue->toDisk.magic != QMAGIC) {
  178.             SM_ERROR(TYPE_FATAL, esmBADMESSAGEMAGIC);
  179.         }
  180. #endif MAGIC_CHECKING  IS_ENABLED
  181.         getMutex(&(DiskQueue->toDisk.mutex));
  182.         if(toDiskEmpty(DiskQueue)) {
  183.             struct     sembuf  _sembuf;
  184.  
  185.             DiskQueue->toDisk.mutex.willBlock=TRUE;
  186.             giveMutex(&(DiskQueue->toDisk.mutex));
  187.             /* 
  188.              * We'll block.  Set a timer so we don't hang around forever
  189.              * should the server die (grot).
  190.              */
  191.             TRPRINT(TR_DISKRW, 0, ("toDisk empty; blocking"));
  192.             alarm(60/*seconds*/ * 1 ); 
  193.  
  194.             /* 
  195.              * wait on the semaphore  for this disk
  196.              */
  197.  
  198.             _sembuf.sem_num = SemNum;
  199.             _sembuf.sem_op  =  -1;  /* a semaphore P operation */
  200.             _sembuf.sem_flg = 0;
  201.  
  202.             TRPRINT(TR_DISKRW, TR_LEVEL_1, 
  203.             ("P on SYSV semaphore #%d, SemId 0x%x",
  204.                 SemNum,SemId));
  205.                  
  206.             {
  207.                 int e;
  208.                 e = semop(SemId, &_sembuf, 1);
  209.                 TRPRINT(TR_DISKRW, TR_LEVEL_1,  
  210.                 ("wakes up with errno %d%s", errno, errno==EINTR?"(EINTR)":""));
  211.                 if( (e < 0 ) && (errno != EINTR)) {
  212.                     SM_ERROR(TYPE_FATAL, errno);
  213.                 }
  214.             }
  215.             alarm(0); 
  216.             getMutex(&(DiskQueue->toDisk.mutex));
  217.             DiskQueue->toDisk.mutex.willBlock=FALSE;
  218.             giveMutex(&(DiskQueue->toDisk.mutex));
  219.             continue; /* loop */
  220.         } else {
  221.  
  222.             /* 
  223.              * il y a quelque chose dans la queue 
  224.              */
  225.             TRPRINT(TR_DISKRW, TR_LEVEL_1, ("dequeueing from toDisk"));
  226.             deq_to(DiskQueue, shmoffset);
  227.             message = ShmOffsetToAddress(shmoffset, DISKMSG);
  228.  
  229.         }
  230.         giveMutex(&(DiskQueue->toDisk.mutex));
  231.  
  232.         /* 
  233.          * don't let the alarm go off during disk i/o 
  234.          */
  235.  
  236.         TRPRINT(TR_DISKRW, TR_LEVEL_1, ("Got message type %d to disk %d", 
  237.             message->header.type, SemNum));
  238.  
  239.  
  240.         /*
  241.          *    check the message to see if the magic is correct
  242.          */
  243.         if (message->diskmagic != DISK_MESSAGE_MAGIC)    {
  244.             
  245.             /*
  246.              *    reply with an error
  247.              */
  248.             fprintf(stderr, 
  249.                 "Possible version mismatch between server and diskrw (%x)\n",
  250.                 SOFTWARE_VERSION);
  251.             SM_ERROR(TYPE_WARNING, esmBADMESSAGEMAGIC);
  252.             message->header.params.out.errno = esmBADMESSAGEMAGIC;
  253.             message->diskmagic = DISK_MESSAGE_MAGIC;
  254.             replyDiskMessage( message );
  255.         }
  256.  
  257.         /*
  258.          *    switch on message type
  259.          */
  260.         switch (message->header.type)    {
  261.  
  262.             case OPEN_DISK:
  263.  
  264.                 openLocalDisk(message, DiskName); /* Dies if unsuccessful */
  265.                 if (rawDeviceSetup() != esmNOERROR) {
  266.                     SM_ERROR(TYPE_STOP, errno);
  267.                 }
  268.                 break;
  269.  
  270.             case CLOSE_DISK:
  271.  
  272.                 closeLocalDisk(message);
  273.                 break;
  274.  
  275.             case READ_BLOCK:
  276.  
  277.                 readLocalDisk(message);
  278.                 break;
  279.  
  280.             case WRITE_BLOCK:
  281.  
  282.                 writeLocalDisk(message);
  283.                 break;
  284.  
  285.             case DUMP_MON:
  286.  
  287.                 dumpLocalProfiling(message);
  288.                 break;
  289.  
  290.             case FSYNC_DISK:
  291.  
  292.                 fsyncLocalDisk(message);
  293.                 break;
  294.  
  295.             default:
  296.                 fprintf(stderr, "unknown header type %d\n", 
  297.                     message->header.type);
  298.  
  299.                 SM_ERROR(TYPE_FATAL, esmINTERNAL);
  300.                 break;
  301.         }
  302.     }
  303. }
  304.